Skip to content

Improve Delivery caching reliability and invalidation - #18

Merged
pokornyd merged 11 commits into
vnextfrom
caching-improvements
Sep 4, 2026
Merged

Improve Delivery caching reliability and invalidation#18
pokornyd merged 11 commits into
vnextfrom
caching-improvements

Conversation

@pokornyd

@pokornyd pokornyd commented Sep 3, 2026

Copy link
Copy Markdown
Member

Improve Delivery caching reliability and invalidation

This PR fixes several issues found during a review of the Delivery caching layer and simplifies some of the related internals.

What changed

Fail-safe behavior

Fail-safe now distinguishes between an unavailable origin and a valid response indicating that no value exists.

  • No response or retryable errors can serve stale content through fail-safe.
  • Non-retryable responses such as 404 return normally and remove the stale cached value.
  • IDeliveryCacheManager keeps the same API; the behavior of null vs. an exception is now documented.

Cache invalidation

  • Fixed invalidations expiring after FusionCache's default 30-second duration.
  • Tag invalidations now use TagsDefaultEntryOptions, which defaults to 10 days and can be configured by consumers.
  • Invalidation keys are normalized and matched case-insensitively.
  • Added DeliveryCacheDependencies.ForItem, ForType, ForTaxonomy, and ForAsset for composing dependency keys.

Cache isolation and Redis

  • FusionCache now owns the cache key prefix through CacheKeyPrefix.
  • Distributed keys are versioned as {KeyPrefix}:{EnvironmentId}:v1:{key}.
  • Purging one client no longer clears other clients sharing the same cache.
  • Redis failures use a short circuit breaker instead of failing cached queries.
  • FusionCache logging is now wired through the standard logging setup.
  • Cache entries declare a size of 1, so size-limited memory caches work correctly.

Existing Redis entries using the previous key format will remain unused until they expire.

Configuration and cache manager access

  • ConfigureFusionCache settings now apply to SDK cache operations.
  • Writes start from the consumer's DefaultEntryOptions and override only SDK-specific settings.
  • The default cache manager can now be resolved without a keyed service.
  • DeliveryClient.CacheManager exposes the cache manager for standalone clients.

Internal cleanup

  • Added CacheResult<T>.IsStale for per-request fail-safe state.
  • Removed the previous event-handler/state-provider approach.
  • Merged MemoryCacheManager and HybridCacheManager into FusionCacheManager.
  • Item and listing queries now share the same cached fetch implementation.

Public API changes

Added:

  • DeliveryClient.CacheManager
  • CacheResult<T>.IsStale
  • DeliveryCacheDependencies.ForItem
  • DeliveryCacheDependencies.ForType
  • DeliveryCacheDependencies.ForTaxonomy
  • DeliveryCacheDependencies.ForAsset

No public APIs were removed.

Consumer-visible behavior

  • A 404 or other non-retryable response after invalidation no longer serves stale content.
  • A 5xx or unavailable origin can still serve stale content when fail-safe is enabled.
  • Redis outages no longer fail cached queries; requests may temporarily fall back to the origin and produce warning logs.
  • ConfigureFusionCache settings such as DefaultEntryOptions now affect SDK operations.
  • Redis cache keys use the new versioned format.

Verification

  • Full solution builds with zero warnings.
  • Test suite passes in both reference modes.
  • Delivery tests increased from 1042 to 1051.
  • Public API snapshots were reviewed for the new additions.

The four Redis-gated integration tests ran in CI against the pushed changes and pass, which matters here because this PR changes Redis key handling and purge behavior.

Documentation was also updated to reflect the new fail-safe behavior, invalidation rules, cache configuration, and multi-client behavior.

@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.93680% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.82%. Comparing base (18a2834) to head (10f9a49).

Files with missing lines Patch % Lines
.../Kontent.Ai.Delivery.Caching/FusionCacheManager.cs 93.33% 3 Missing and 4 partials ⚠️
...ent.Ai.Delivery/Api/QueryBuilders/TaxonomyQuery.cs 50.00% 2 Missing and 1 partial ⚠️
...Kontent.Ai.Delivery/Api/QueryBuilders/TypeQuery.cs 50.00% 2 Missing and 1 partial ⚠️
...t.Ai.Delivery/Api/QueryBuilders/TaxonomiesQuery.cs 77.77% 2 Missing ⚠️
...ontent.Ai.Delivery/Api/QueryBuilders/TypesQuery.cs 77.77% 2 Missing ⚠️
...Kontent.Ai.Delivery/Api/QueryBuilders/ItemQuery.cs 94.44% 0 Missing and 1 partial ⚠️
...ontent.Ai.Delivery/Api/QueryBuilders/ItemsQuery.cs 94.44% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##            vnext      #18      +/-   ##
==========================================
+ Coverage   90.74%   90.82%   +0.07%     
==========================================
  Files         509      509              
  Lines       10042     9922     -120     
  Branches      947      937      -10     
==========================================
- Hits         9113     9012     -101     
+ Misses        606      589      -17     
+ Partials      323      321       -2     
Flag Coverage Δ
aspnetcore 90.82% <92.93%> (+0.07%) ⬆️
delivery 90.82% <92.93%> (+0.07%) ⬆️
management 90.82% <92.93%> (+0.07%) ⬆️
model-generator 90.82% <92.93%> (+0.07%) ⬆️
sync 90.82% <92.93%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ent.Ai.Delivery.Abstractions/Caching/CacheEntry.cs 100.00% <ø> (ø)
...nt.Ai.Delivery.Abstractions/Caching/CacheResult.cs 100.00% <100.00%> (ø)
....Abstractions/Caching/DeliveryCacheDependencies.cs 100.00% <100.00%> (ø)
...ivery.Abstractions/Caching/DeliveryCacheOptions.cs 100.00% <ø> (ø)
...very.Abstractions/Caching/IDeliveryCacheManager.cs 100.00% <ø> (ø)
...ntentItems/Processing/DependencyTrackingContext.cs 100.00% <100.00%> (+7.14%) ⬆️
...ching/Extensions/DeliveryCacheOptionsExtensions.cs 100.00% <ø> (+100.00%) ⬆️
...tensions/DeliveryClientBuilderCachingExtensions.cs 100.00% <100.00%> (ø)
...very/Api/QueryBuilders/Helpers/CachedItemsFetch.cs 100.00% <100.00%> (ø)
...y/Api/QueryBuilders/Helpers/CachedQueryExecutor.cs 100.00% <100.00%> (ø)
... and 9 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pokornyd
pokornyd merged commit c5def31 into vnext Sep 4, 2026
10 checks passed
@pokornyd
pokornyd deleted the caching-improvements branch September 4, 2026 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant